﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using PowerHub.MES.Entities;
using PowerHub.MES.ReturnEntity;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace PowerHub.MES.EFBase
{
    /// <summary>
    /// 数据通用层ef封装
    /// </summary>
    public class BaseRepository<TEntity> where TEntity : BaseEntity
    {
        PowerHubMesContext _coreShopContext;

        public BaseRepository(PowerHubMesContext repositorySys)
        {
            _coreShopContext = repositorySys;
        }
        /// <summary>
        /// 获取数据 
        /// </summary>
        /// <returns></returns>
        public DbSet<TEntity> GetEntitys()
        {
            return _coreShopContext.Set<TEntity>();
        }

        /// <summary>
        /// 获取单条数据 
        /// </summary>
        /// <returns></returns>
        public TEntity GetEntitySingle(string Id)
        {
            return _coreShopContext.Set<TEntity>().FirstOrDefault(g => g.Id.Equals(Id));
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public int UpdataEntity(TEntity entity)
        {
            entity.UpdateDate = DateTime.Now;
            _coreShopContext.Set<TEntity>().Update(entity);
            return _coreShopContext.SaveChanges();
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public int AddEntity(TEntity entity)
        {
            entity.Createtime = DateTime.Now;
            _coreShopContext.Set<TEntity>().Add(entity);
            return _coreShopContext.SaveChanges();
        }


        /// <summary>
        /// 批量新增实体集合
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns>受影响的行数</returns>
        public int AddEntitieRange(List<TEntity> entities)
        {
            entities.ForEach(w => w.Createtime = DateTime.Now);
            _coreShopContext.Set<TEntity>().AddRange(entities);
            return _coreShopContext.SaveChanges();
        }


        /// <summary>
        /// 批量更新实体集合
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public int UpdataEntityRange(List<TEntity> entities)
        {
            entities.ForEach(w => w.UpdateDate = DateTime.Now);
            _coreShopContext.Set<TEntity>().UpdateRange(entities);
            return _coreShopContext.SaveChanges();
        }

        /// <summary>
        /// 批量删除实体集合
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns>受影响的行数</returns>
        public int DeleteEntityRange(List<TEntity> entities)
        {
            entities.ForEach(w => w.UpdateDate = DateTime.Now);
            _coreShopContext.Set<TEntity>().RemoveRange(entities);
            return _coreShopContext.SaveChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public int DeleteEntity(string Id)
        {
            TEntity entity = _coreShopContext.Set<TEntity>().FirstOrDefault(i => Id.EndsWith(Id));
            if (entity != null)
            {
                _coreShopContext.Set<TEntity>().Remove(entity);
            }
            return _coreShopContext.SaveChanges();
        }



        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="where"></param>
        /// <param name="query"></param>
        /// <param name="include"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        internal async Task<OperationPageResult<List<TEntity>>> GetEFCoreBasePageAsync<TEntity>(Expression<Func<TEntity, bool>> where, PageQuery query, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include) where TEntity : class
        {
            throw new NotImplementedException();
        }
    }
}
